// pages/my_message/index.js
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import { debounce, timeDifference} from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    notice: [],
    noNotice: false,
    privateLetters: [],
    noPrivateLetters: false,
    page: 1,
    size: 10,
    page2: 1,
    scrollTop: 0,
    scrollTop2: 0
  },
  async getNotice() {
    let { page, size , noNotice, notice} = this.data
    const {
      code,
      data,
      msg
    } = await api.send({
      flag: '/letter/getSysNotice',
      page,
      size
    })
    if (code === 1) {

      if(data.list.length < size) {
        noNotice = true
      }else {
        page ++
      }
      data.list.forEach(item => {
        item.time = timeDifference(item.create_time)
      })
      notice.push(...data.list)
      this.setData({
        notice,
        noNotice,
        page
      })

    } else {
      Toast(msg)
    }
  },
  async getPrivateLetters() {
    let { page2, size , noPrivateLetters, privateLetters} = this.data
    const {
      code,
      data,
      msg
    } = await api.send({
      flag: '/letter/getReceiveLatter',
      page: page2,
      size
    })
    if (code === 1) {

      if(data.list.length < size) {
        noPrivateLetters = true
      }else {
        page2 ++
      }

      privateLetters.push(...data.list)
      this.setData({
        privateLetters,
        noPrivateLetters,
        page2
      })

    } else {
      Toast(msg)
    }
  },

  onClose(event) {
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？',
        }).then(() => {
          instance.close();
        });
        break;
    }
  },
  onChange (e) {
    const { notice, privateLetters, scrollTop, scrollTop2 } = this.data
    const { index } = e.detail
    if(index == 0) {
      wx.pageScrollTo({
        selector: '#notice',
        scrollTop: scrollTop,
        duration: 0
      })
      if(!notice.length) {
        this.getNotice()
      }
    }else {
      wx.pageScrollTo({
        selector: '#private_letters',
        scrollTop: scrollTop2,
        duration: 0
      })
      if(!privateLetters.length) {
        this.getPrivateLetters()
      }
    }
    this.setData({active: index})
  },
  onScroll: debounce(function(e){
    const { scrollTop } = e[0].detail || 0
    const { active } = this.data
    if(active == 0) {
      this.setData({ scrollTop })
    }else {
      this.setData({ scrollTop2: scrollTop })
    }
  },200),
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNotice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { active, noNotice, noPrivateLetters } = this.data
    if(active == 0 && !noNotice)  {
      this.getNotice()
    }else if(active == 1 && !noPrivateLetters) {
      this.getPrivateLetters()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})